<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/13
 * Time: 18:02
 */
namespace app\shop;


use app\common\Common;
use app\data\OrderData;
use app\data\ShopData;
use app\order\Order;
use think\Db;

class ShopGoods extends Base
{
    /**
     * 商品名字长度限制
     * @var array
     */
    private $goodsName = [1, 150];

    /**
     * 排序字段最大值
     * @var int
     */
    private $sortMax = 65535;

    /**
     * 砍价商品排序
     * @var array
     */
    private $bargainSort = ['create_time'];

    private $searchGoods = ['goods_price', 'order_rating', 'goods_num'];


    /**
     * 排序方法
     * @var array
     */
    private $sortType = ['desc', 'asc'];

    /**
     * 商品排序
     * @var array
     */
    private $sortGoods = ['create_time'];
    /**
     * 分享长度设置
     * @var array
     */
    private $titleMax = [1, 20];

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
    }

    private function checkShopVerify($shop_id)
    {
        $res = Common::getTableValue('drh_shop', 'verify_status', 'id', $shop_id);
        if ($res != 1) {
            return ['code' => $this->f_code, 'msg' => '商户正在审核,请稍后在试'];
        }
        return ['code' => $this->t_code];
    }

    /**
     * @param $shop_id
     * @param $goods_name
     * @param $goods_price
     * @param $give_integral
     * @param $goods_type_id
     * @param $goods_group
     * @param $is_shelves
     * @param $goods_stock
     * @param $goods_hot
     * @param $goods_new
     * @param $goods_freight
     * @param $goods_img
     * @param $goods_info
     * @param $is_proxy
     * @param $distribution_status
     * @param $bargain_status
     * @param $group_buy_status
     * @param $game_status
     * @param $proxy_goods_value
     * @return array
     */
    private function check($shop_id, $goods_name, $goods_price, $give_integral, $goods_type_id, $proxy_goods_value, $goods_group,
                           $is_shelves, $goods_stock, $goods_hot, $goods_new, $goods_freight, $goods_img, $goods_info
        , $is_proxy, $distribution_status, $bargain_status, $group_buy_status, $game_status)
    {
        if (Common::checkTableFields('drh_shop', 'id', $shop_id) == $this->f_code) {
            return ['code' => $this->f_code, 'msg' => '不存在该商户'];
        }
        if (!is_numeric($proxy_goods_value) || ($proxy_goods_value > 100 || $proxy_goods_value < 0)) {
            return ['code' => $this->f_code, 'msg' => '分销比例设置错误'];
        }
        $res = $this->checkShopVerify($shop_id);
        if ($res['code'] == $this->f_code) {
            return $res;
        }
        if (Common::checkTableFieldsValue('drh_goods_type', 'shop_id', 'id', $goods_type_id, $shop_id) == $this->f_code) {
            return ['code' => $this->f_code, 'msg' => '不存在该分类'];
        }
        $check = ['商品分类' => ["{$this->goodsName[0]},{$this->goodsName[1]}}", $goods_name],];
        $res = Common::checkLength($check);
        if ($res['code'] == $this->f_code) {
            return $res;
        }
        $arr = [$goods_price, $give_integral, $goods_stock, $goods_hot, $goods_new, $goods_freight];

        foreach ($arr as $k => $v) {
            if (!Common::checkNumber($v, 0)) {
                return ['code' => $this->f_code, 'msg' => '输入数字不合法'];
            }
        }
        if ($goods_group != 0 && $goods_group != 1 && $goods_group != 2) {
            return ['code' => $this->f_code, 'msg' => '商品分组错误'];
        }
        if ($is_shelves != 0 && $is_shelves != 1 && $is_shelves != 2) {
            return ['code' => $this->f_code, 'msg' => '是否上架填写错误'];
        }
        if ($goods_new > $this->sortMax || $goods_hot > $this->sortMax) {
            return ['code' => $this->f_code, 'msg' => '超过最大排序值'];
        }
        if ($is_proxy != 0 && $is_proxy != 1) {
            return ['code' => $this->f_code, 'msg' => '代理关系错误'];
        }
        if (count($goods_img) > 30 || count($goods_info) > 30) {
            return ['code' => $this->f_code, 'msg' => '商品图片或详情图片最多不超过30张'];
        }

        if (!in_array($distribution_status, [0, 1]) ||
            !in_array($bargain_status, [0, 1]) ||
            !in_array($group_buy_status, [0, 1]) ||
            !in_array($game_status, [0, 1])
        ) {
            return ['code' => $this->t_code, 'msg' => '参数数据错误'];
        }
        return ['code' => $this->t_code];
    }

    /**
     * 添加商品
     * @param $shop_id
     * @param $goods_name
     * @param $goods_price
     * @param $give_integral
     * @param $goods_type_id
     * @param int $goods_group
     * @param int $is_shelves
     * @param $goods_stock
     * @param int $goods_hot
     * @param int $goods_new
     * @param int $goods_freight
     * @param array $goods_attr_value
     * @param array $goods_info
     * @param array $goods_img
     * @param  $is_proxy
     * @param  $proxy_goods_value
     * @param  $distribution_status
     * @param  $bargain_status
     * @param  $group_buy_status
     * @param  $game_status
     * @return \think\response\Json
     */
    public function addGoods($shop_id, $goods_name, $goods_price, $give_integral, $goods_type_id, $proxy_goods_value, $goods_stock, $goods_freight, $is_proxy,
                             $goods_group = 0, $is_shelves = 1, $goods_hot = 1000, $goods_new = 1000,
                             array $goods_attr_value = [], array $goods_info = [], array $goods_img = [],
                             $distribution_status, $bargain_status, $group_buy_status, $game_status)
    {
        $price = $goods_price;
        if ($goods_price == 0) {
            $price = 1;
        }
        $res = $this->check($shop_id, $goods_name, $price, $give_integral, $goods_type_id, $proxy_goods_value,
            $goods_group, $is_shelves, $goods_stock, $goods_hot, $goods_new, $goods_freight, $goods_img, $goods_info,
            $is_proxy, $distribution_status, $bargain_status, $group_buy_status, $game_status);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }

        $shop = new ShopData();
        $data = $shop->addGoods($shop_id, $goods_name, $goods_price, $give_integral, $goods_type_id,
            $goods_group, $is_shelves, $goods_stock, $goods_hot, $goods_new,
            json_encode($goods_attr_value), json_encode($goods_info), json_encode($goods_img), $goods_freight, $is_proxy, $proxy_goods_value);
        $this->addGoodsPlugin($data['id'], $distribution_status, $bargain_status, $group_buy_status, $game_status, $shop);
        return json($data);
    }

    private function addGoodsPlugin($goods_id, $distribution_status, $bargain_status, $group_buy_status,
                                    $game_status, ShopData $shop)
    {
        $time = time();
        $shop->addGoodsPlugin($goods_id, $distribution_status, $bargain_status, $group_buy_status, $game_status, $time);
    }

    /**
     * 修改商品属性
     * @param $id
     * @param $shop_id
     * @param $goods_name
     * @param $goods_price
     * @param $give_integral
     * @param $goods_type_id
     * @param int $goods_group
     * @param int $is_shelves
     * @param $goods_stock
     * @param int $goods_hot
     * @param int $goods_new
     * @param int $goods_freight
     * @param array $goods_attr_value
     * @param array $goods_info
     * @param array $goods_img
     * @param  $is_proxy
     * @param  $proxy_goods_value
     * @param  $distribution_status
     * @param  $bargain_status
     * @param  $group_buy_status
     * @param  $game_status
     * @return \think\response\Json
     */
    public
    function edit($id, $shop_id, $goods_name, $goods_price, $give_integral, $goods_type_id, $proxy_goods_value,
                  $goods_group = 0, $is_shelves = 1, $goods_stock, $goods_hot = 1000, $goods_new = 1000,
                  array $goods_attr_value = [], array $goods_info = [], array $goods_img = [], $goods_freight,
                  $is_proxy, $distribution_status, $bargain_status, $group_buy_status, $game_status)
    {
        $price = $goods_price;
        if ($goods_price == 0) {
            $price = 1;
        }
        $res = $this->check($shop_id, $goods_name, $price, $give_integral, $goods_type_id, $proxy_goods_value,
            $goods_group, $is_shelves, $goods_stock, $goods_hot, $goods_new, $goods_freight, $goods_img, $goods_info,
            $is_proxy, $distribution_status, $bargain_status, $group_buy_status, $game_status);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        if (Common::checkTableFieldsValue('drh_goods', 'shop_id', 'id', $id, $shop_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '不存在该商品']);
        }
        $res = Common::getTableValue('drh_goods', 'proxy_goods_id', ['id', 'shop_id'], [$id, $shop_id]);
        if ($res > 0) {
            return json(['code' => $this->f_code, 'msg' => '不能修改代理商品']);
        }
        $shop = new ShopData();
        Db::startTrans();
        try {
            $res = $shop->editGoods($id, $shop_id, $goods_name, $goods_price, $give_integral, $goods_type_id,
                $goods_group, $is_shelves, $goods_stock, $goods_hot, $goods_new,
                json_encode($goods_attr_value), json_encode($goods_info), json_encode($goods_img), $goods_freight, $is_proxy, $proxy_goods_value);
            if ($is_proxy == 0 || $is_shelves >= 2) {//不代理
                $this->cancelGoodsProxy($id);//删除所有的代理商品
            } else {//修改代理商品属性
                $shop->editProxyGoods($id, $goods_name, $goods_price, $give_integral, $goods_type_id,
                    $goods_group, $is_shelves, $goods_stock, $goods_hot, $goods_new,
                    json_encode($goods_attr_value), json_encode($goods_info), json_encode($goods_img), $goods_freight, $is_proxy);
            }
            Db::commit();
            $this->editGoodsPlugin($id, $distribution_status, $bargain_status, $group_buy_status, $game_status, $shop);
            return json($res);
        } catch (\Exception $e) {
            Db::rollback();
            return json(['code' => $this->f_code, 'msg' => $e->getMessage()]);
        }
    }

    private function editGoodsPlugin($goods_id, $distribution_status, $bargain_status, $group_buy_status,
                                     $game_status, ShopData $shop)
    {
        $res = Common::getTableValue('drh_goods_plugin', 'id', ['goods_id'], [$goods_id]);
        if (empty($res)) {
            $time = time();
            $shop->addGoodsPlugin($goods_id, $distribution_status, $bargain_status, $group_buy_status, $game_status, $time);
        } else {
            $shop->editGoodsPlugin($goods_id, $distribution_status, $bargain_status, $group_buy_status, $game_status);
        }
    }

    /**
     * 删除商品
     * @param $id
     * @param $shop_id
     * @param $is_shelves
     * @return \think\response\Json
     */
//    public  function delGoods($id, $shop_id, $is_shelves)
//    {
//        if (Common::checkTableFieldsValue('drh_goods', 'shop_id', 'id', $id, $shop_id) == $this->f_code) {
//            return json(['code' => $this->f_code, 'msg' => '不存在该商品']);
//        }
//        $res = $this->checkShopVerify($shop_id);
//        if ($res['code'] == $this->f_code) {
//            return json($res);
//        }
//        $order = new Order();
//        if ($is_shelves != 0 && $is_shelves != 1 && $is_shelves != 2 && $is_shelves != 3) {
//            return json(['code' => $this->f_code, 'msg' => '操作不合法']);
//        }
//        $count = $order->checkTransactionOrder($id);
//        if ($count > 0) {
//            return json(['code' => $this->f_code, 'msg' => '商品正在交易无法删除该商品']);
//        }
//        $shop = new ShopData();
//        if ($is_shelves >= 2) {//代理商品删除
//            $this->cancelGoodsProxy($id);
//        }
//        return json($shop->delGoods($id, $shop_id));
//    }
        public function delGoods($id,$is_shelves,$ditch=0,$shop_id = -1)
        {
            switch ($ditch) {
                case 0:
                    if($shop_id == -1)   return json(['code' => $this->f_code, 'msg' => 'shop_id错误']);
                    if (Common::checkTableFieldsValue('drh_goods', 'shop_id', 'id', $id, $shop_id) == $this->f_code) {
                        return json(['code' => $this->f_code, 'msg' => '不存在该商品']);
                    }
                    $order = new Order();
                    if ($is_shelves != 0 && $is_shelves != 1 && $is_shelves != 2) {
                        return json(['code' => $this->f_code, 'msg' => '操作不合法']);
                    }
                    $count = $order->checkTransactionOrder($id);
                    if ($count > 0) {
                        return json(['code' => $this->f_code, 'msg' => '商品正在交易无法删除该商品']);
                    }
                    $shop = new ShopData();
                    if ($is_shelves >= 2) {//代理商品删除
                        $this->cancelGoodsProxy($id);
                    }
                    return json($shop->delGoods($id, $shop_id));
                    break;
                case 1;
                    Common::db_upload("drh_goods","is_shelves=3 WHERE id=".$id);
                    return json(['code' => $this->t_code, 'msg' => '操作成功']);
                    break;
                default:
                    break;
            }
        }
    /**
     * 商家后端 商品列表展示
     * @param $shop_id
     * @param $page
     * @param $pre
     * @param array $sort
     * @param int $is_shelves
     * @return \think\response\Json
     */
    public function showGoodsList($shop_id, $page = 1, $pre = 15, array $sort = ['create_time' => 'desc'], $is_shelves = 1)
    {
        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->sortGoods, $this->sortType, $sort);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $res = $this->checkShopVerify($shop_id);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $shop = new ShopData();
        $data = $shop->showAdminGoodsList($shop_id, $page, $pre, $sort, $is_shelves);
        foreach ($data as $k=>$v){
            $data[$k]['goods_img'] = json_decode($v['goods_img'])??unserialize($v['goods_img'])??$v['goods_img'];
            $data[$k]['goods_info'] = json_decode($v['goods_info'])??unserialize($v['goods_info'])??$v['goods_info'];
            $data[$k]['goods_attr_value']= json_decode($v['goods_attr_value'])??unserialize($v['goods_attr_value'])??$v['goods_attr_value'];
        }
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $data,
                'total' => $shop->total(" shop_id={$shop_id} and is_shelves={$is_shelves} ", 'drh_goods')
            ]
        ]);
    }
    //供货商商品列表
    public function  supply_of_goods($shop_id, $page = 1, $pre = 15, array $sort = ['create_time' => 'desc'], $is_shelves = 1){
        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->sortGoods, $this->sortType, $sort);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $res = $this->checkShopVerify($shop_id);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }

         $two =  Common::er_array(Db::table('drh_goods_proxy')
            ->where('shop_id',$shop_id)
             ->where('is_not',0)
            ->field('goods_id')
            ->select());

        if($two !== array()){
            $good_id =  implode(",",$two);
            $shop = new ShopData();
        $count = Db::table('drh_goods a')
            ->join('drh_goods_proxy t','t.shop_id=a.id','left')
            ->join('drh_goods_type b', 't.goods_type_id=b.id', 'left')
            ->join('drh_goods_plugin c', 'a.id=c.goods_id', 'left')
            ->where('a.is_shelves', $is_shelves)
            ->where('is_ware',1)
            ->where('a.id','in',$good_id)
            ->count();
            $data = $shop->showAdminGoodsList_two($good_id, $page, $pre, $sort, $is_shelves);
          $data = $this->getImage_two($data);
            return json([
                'code' => $this->t_code,
                'msg' => 'OK',
                'data' => [
                    'list' => $data??[],
                    'total' =>$count??0
                ]
            ]);
        }
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => [],
                'total' =>0
            ]
        ]);
}
    /**
     * @param $shop_id
     * @param $goods_id
     * @param $type_id
     * @return \think\response\Json
     */
    public function changeGoodsType($shop_id, $goods_id, $type_id)
    {
        $res = Common::getTableValue('drh_goods_type', 'id', ['id', 'shop_id'], [$type_id, $shop_id]);
        if (empty($res)) {
            return json(['code' => $this->f_code, 'msg' => '不存在该分类']);
        }
        $res = Common::getTableValue('drh_goods', 'id', ['id', 'shop_id'], [$goods_id, $shop_id]);
        if (empty($res)) {
            return json(['code' => $this->f_code, 'msg' => '不存在该商品']);
        }
        $shop = new ShopData();
        $shop->changeGoodsType($shop_id, $goods_id, $type_id);
        return json([
            'code' => $this->t_code,
            'msg' => '修改成功'
        ]);
    }

    private function getImage($data)
    {
        foreach ($data as $k => $v) {
            $goods_img = json_decode($v['goods_img'], true)??unserialize($v['goods_img']);
            $goods_info = empty($v['goods_info']) ? '' : json_decode($v['goods_info'], true)??unserialize($v['goods_info']);
            $goods_img = empty($goods_img[0]) ? '' : $goods_img[0];
            $data[$k]['goods_img'] = $goods_img;
            $data[$k]['goods_info'] = $goods_info;
        }
        return $data;
    }
    private function getImage_two($data)
    {
        foreach ($data as $k => $v) {
            $data[$k]['goods_attr_value'] = unserialize($v['goods_attr_value']);
            $data[$k]['goods_img'] = unserialize($v['goods_img']);
        }
        return $data;
    }
    private function getGoodsPrice($data)
    {
        foreach ($data as $k => $v) {
            $attr_value = json_decode($v['goods_attr_value'], true);
            $data[$k]['goods_attr_value'] = $attr_value;
        }
        return $data;
    }

    /**
     * 展示商户商品详情
     * @param $shop_id
     * @param $goods_id
     * @return mixed
     */
    public function showGoodsInfo($shop_id, $goods_id)
    {
        $res = Common::getTableValue('drh_goods', 'id', ['id'], [$goods_id]);
        if (empty($res)) {
            return json(['code' => $this->f_code, 'msg' => '不存在该商品']);
        }
        $res = $this->checkShopVerify($shop_id);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $shop = new ShopData();
        $res =Db::table('drh_goods')
            ->where([
                'id' => $goods_id
            ])->find();
        $data = Common::getTableValue('drh_goods_plugin', ['distribution_status', 'bargain_status', 'group_buy_status', 'game_status']
            , ['goods_id'], [$goods_id]);
        $res['distribution_status'] = empty($data['distribution_status'])?1:$data['distribution_status'];
        $res['bargain_status'] = empty($data['bargain_status'])?1:$data['bargain_status'];
        $res['group_buy_status'] = empty($data['group_buy_status'])?1:$data['group_buy_status'];
        $res['game_status'] = empty($data['game_status'])?1:$data['game_status'];
        unset($res['shelves_time']);
        unset($res['off_shelves_time']);
        unset($res['create_time']);
        unset($res['platform_lv']);
        $res['goods_info'] = json_decode($res['goods_info'])??$res['goods_info'];
        $res['goods_img'] = json_decode($res['goods_img'])??unserialize($res['goods_img']);
        $res['goods_attr_value'] = json_decode($res['goods_attr_value'], true)??unserialize($res['goods_attr_value']);
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => $res
        ]);
    }


    /**
     * 基本校验
     * @param $shop_id
     * @param $goods_id
     * @param $bargain_total_money
     * @param $bargain_once_money
     * @param $title
     * @param $goods_attr_value
     * @return array|\think\response\Json
     */
    public function checkBargainGoods($shop_id, $goods_id, $bargain_total_money, $bargain_once_money, $title, $goods_attr_value)
    {
        $res = $this->checkShopVerify($shop_id);
        if ($res['code'] == $this->f_code) {
            return $res;
        }
        $res = Common::getTableValue('drh_goods', ['goods_price', 'proxy_goods_id'], ['id', 'shop_id'], [$goods_id, $shop_id]);
        if($res == null){
            $res =  Db::name('drh_goods_proxy a')->join('drh_goods b','a.goods_id=b.id','left')->where("goods_id={$goods_id} and a.shop_id={$shop_id} and is_not=0")->field('goods_price,proxy_goods_id')->find();
        }

        if ($res['proxy_goods_id'] != 0) {
            return ['code' => $this->f_code, 'msg' => '分销商品不能进行砍价设置'];
        }

        $goods_money = $res['goods_price'];
        if (empty($goods_money)) {
            return ['code' => $this->f_code, 'msg' => '不存在该商品'];
        }
        $res = Common::getTableValue('drh_goods', ['goods_stock', 'goods_attr_value'], 'id', $goods_id);
        $goods_attr_values = json_decode($res['goods_attr_value'], true);
        if($goods_attr_value == ""){
            $goods_attr_value = unserialize($res['goods_attr_value']);
        }

        $money = Common::checkGoodsAttr($goods_attr_values, $goods_attr_value);
//        if ($money < 0) {
//            return ['code' => $this->f_code, 'msg' => '不存在该商品属性'];
//        }
        if ($bargain_total_money > $goods_money + $money) {
            return ['code' => $this->f_code, 'msg' => '砍价金额不能高于商品价格'];
        }
        if (!Common::checkNumber($bargain_once_money)) {
            return ['code' => $this->f_code, 'msg' => '单次砍价金额错误'];
        }
        if (!Common::checkNumber($bargain_total_money) || !Common::checkNumber($bargain_once_money)) {
            return ['code' => $this->f_code, 'msg' => '砍价金额错误'];
        }
        $check = ['分享设置' => ["{$this->titleMax[0]},{$this->titleMax[1]}}", $title],];
        $res = Common::checkLength($check);
        if ($res['code'] == $this->f_code) {
            return $res;
        }

        return ['code' => $this->t_code];
    }


    /**
     * 创建砍价商品
     * @param $shop_id
     * @param $goods_id
     * @param $bargain_total_money
     * @param $bargain_once_money
     * @param $title
     * @param $goods_attr_value
     * @return \think\response\Json
     */
    public function createBargainGoods($shop_id, $goods_id, $bargain_total_money, $bargain_once_money, $title, $goods_attr_value)
    {
        $res = Common::getTableValue('drh_goods_bargain', 'shop_id', ['shop_id', 'goods_id'], [$shop_id, $goods_id]);

        if (!empty($res)) {
            return json(['code' => $this->f_code, 'msg' => '请勿重复创建']);
        }
        $res = Common::checkShopOrGoodsPlugin($shop_id, $goods_id, ['bargain_status']);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }

        $res = Common::getTableValue('drh_shop', 'verify_status', 'id', $shop_id);

        if ($res != 1) {
            return json(['code' => $this->f_code, 'msg' => '商户正在审核,请稍后在试']);
        }


        $res = $this->checkBargainGoods($shop_id, $goods_id, $bargain_total_money,
            $bargain_once_money, $title, $goods_attr_value);


        if ($res['code'] == $this->f_code) {
            return json($res);
        }

        $shop = new ShopData();
        return json($shop->createBargainGoods($shop_id, $goods_id,
            $bargain_total_money, $bargain_once_money, $title, $goods_attr_value));
    }


    /**
     * 修改砍价商品
     * @param $shop_id
     * @param $goods_id
     * @param $bargain_total_money
     * @param $bargain_once_money
     * @param $title
     * @param $goods_attr_value
     * @return \think\response\Json
     */
    public
    function editBargainGoods($shop_id, $goods_id, $bargain_total_money, $bargain_once_money, $title, $goods_attr_value)
    {
        $res = Common::checkShopOrGoodsPlugin($shop_id, $goods_id, ['bargain_status']);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $res = $this->checkBargainGoods($shop_id, $goods_id, $bargain_total_money, $bargain_once_money, $title, $goods_attr_value);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $res = Common::getTableValue('drh_shop', 'verify_status', 'id', $shop_id);
        if ($res != 1) {
            return json(['code' => $this->f_code, 'msg' => '商户正在审核,请稍后在试']);
        }
        $shop = new ShopData();
        return json($shop->editBargainGoods($shop_id, $goods_id, $bargain_total_money * 100,
            $bargain_once_money * 100, $title, $goods_attr_value));
    }

    /**
     * 删除砍价商品
     * @param $shop_id
     * @param $goods_id
     * @return \think\response\Json
     */
    public
    function delBargainGoods($shop_id, $goods_id)
    {
        $res = $this->checkShopVerify($shop_id);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $res = Common::getTableValue('drh_shop', 'verify_status', 'id', $shop_id);
        if ($res != 1) {
            return json(['code' => $this->f_code, 'msg' => '商户正在审核,请稍后在试']);
        }
        $shop = new ShopData();
        return json($shop->delBargainGoods($shop_id, $goods_id));
    }

    /**
     * 展示砍价商品列表
     * @param $shop_id
     * @param int $page
     * @param int $pre
     * @param array $sort
     * @return array|\think\response\Json
     */
    public
    function showBargainGoods($shop_id, $page = 1, $pre = 15, array $sort = ['create_time' => 'desc'])
    {
        $res = Common::checkShopOrGoodsPlugin($shop_id, '', ['bargain_status']);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->bargainSort, $this->sortType, $sort);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $res = $this->checkShopVerify($shop_id);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        if (Common::checkTableFields('drh_shop', 'id', $shop_id) == $this->f_code) {
            return ['code' => $this->f_code, 'msg' => '不存在该商户'];
        }
        $shop = new ShopData();
        $data = $shop->showBargainGoods($shop_id, $page, $pre, $sort);

        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $data,
                'total' => $shop->total('shop_id = ' . $shop_id, 'drh_goods_bargain'),
            ]
        ]);
    }

    /**
     * 展示固定砍价商品的信息
     * @param $shop_id
     * @param $goods_id
     * @param $goods_value
     * @return array|\think\response\Json
     */
    public
    function showBargainGood($shop_id, $goods_id, $goods_value)
    {
        $res = Common::checkShopOrGoodsPlugin($shop_id, $goods_id, ['bargain_status']);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $goods_money = Common::getTableValue('drh_goods_bargain', 'goods_id',
            ['goods_id', 'shop_id'], [$goods_id, $shop_id]);
        if (empty($goods_money)) {
            return ['code' => $this->f_code, 'msg' => '不存在该商品'];
        }
        $res = $this->checkShopVerify($shop_id);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $shop = new ShopData();
        $where = "shop_id ={$shop_id} and goods_id={$goods_id} and goods_attr ='{$goods_value}'";
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => $shop->showBargainGood($where),
        ]);
    }

    /**
     * 添加代理商品
     * @param $goods_id
     * @param $proxy_shop_id
     * @param $shop_id
     * @param $user_id
     * @return array|\think\response\Json
     */
    public function addProxyGoods($goods_id, $proxy_shop_id, $shop_id, $user_id)
    {
        $res = Common::checkShopOrGoodsPlugin($shop_id, $goods_id, ['distribution_status']);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $res = $this->checkShopVerify($shop_id);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $goods_money = Common::getTableValue('drh_goods', 'id',
            ['id', 'shop_id', 'is_proxy', 'is_shelves'], [$goods_id, $proxy_shop_id, 1, 1]);
        if (empty($goods_money)) {
            return json(['code' => $this->f_code, 'msg' => '该商品无法代理']);
        }
        //校验是否重复代理
        $goods_money = Common::getTableValue('drh_goods', 'id',
            ['proxy_goods_id', 'shop_id'], [$goods_id, $shop_id]);
        if (!empty($goods_money)) {
            return json(['code' => $this->f_code, 'msg' => '请勿重复代理']);
        }
        if (empty($goods_money)) {
            return json(['code' => $this->f_code, 'msg' => '该商品无法代理']);
        }
        if (Common::checkTableFieldsValue('drh_shop', 'user_id', 'id', $shop_id, $user_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '不存在该商户']);
        }
        $shop = new ShopData();
        $info = $shop->showAdminGoodsInfo($proxy_shop_id, $goods_id);
        return json($shop->addProxyGoods($shop_id, $info));
    }

    /**
     * 展示有代理商品商户
     * @param int $page
     * @param int $pre
     * @param array $sort
     * @return \think\response\Json
     */
    public function showProxyShop($page = 1, $pre = 15, array $sort = ['create_time' => 'asc'])
    {
        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->bargainSort, $this->sortType, $sort);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $shop = new ShopData();
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $shop->showProxyShop($page, $pre, $sort),
                'total' => $shop->showShopTotal()
            ]
        ]);
    }

    /**
     * 展示有代理商品商户下的代理商品
     * @param $shop_id
     * @param int $page
     * @param int $pre
     * @param array $sort
     * @return array|\think\response\Json
     */
    public function showProxyGoods($shop_id, $page = 1, $pre = 15, array $sort = ['create_time' => 'desc'])
    {
        $res = Common::checkShopOrGoodsPlugin($shop_id, '', ['distribution_status']);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->bargainSort, $this->sortType, $sort);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $res = $this->checkShopVerify($shop_id);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        if (Common::checkTableFields('drh_shop', 'id', $shop_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '不存在该商户']);
        }
        $shop = new ShopData();


        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $shop->showShopProxyGoods($shop_id, $page, $pre, $sort),
                'total' => $shop->showShopProxyGoodsTotal($shop_id)
            ]
        ]);
    }

    public function showAllGoodsList($shop_id)
    {
        if (Common::checkTableFields('drh_shop', 'id', $shop_id) == $this->f_code) {
            return json(['code' => $this->f_code, 'msg' => '不存在该商户']);
        }
        $res = $this->checkShopVerify($shop_id);
        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        $shop = new ShopData();
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => [
                'list' => $shop->showAllGoodsList($shop_id),
                'total' => $shop->combine($shop_id)
            ]
        ]);
    }


    public function cancelGoodsProxy($goods_id)
    {
        $shop = new ShopData();
        $shop->cancelGoodsProxy($goods_id);
        return true;
    }

///////////////////////////////////-----------------新增功能--------------------------------/////////////////////////

    /**
     * 搜索商品
     * @param string $name
     * @param array $sort
     * @param int $page
     * @param int $pre
     * @return \think\response\Json
     */
    public function searchGoods($name = '',$goods_num=null, $desc=0, $page = 1, $pre = 15)
    {
        //校验页码数和页码条数已经排序
        $res = Common::checkShow($page, $pre, $this->searchGoods, $this->sortType);

        if ($res['code'] == $this->f_code) {
            return json($res);
        }
        if (mb_strlen($name) > 1000) {
            return json(['code' => $this->f_code, 'msg' => '搜索名称太长']);
        }
        $shop = new ShopData();
        $desc = $desc==1?'desc':'';
        $goods_num = !empty($goods_num)?"{$goods_num}":'a.id';

        $data = $shop->searchGoods_prox($name, $page, $pre,$goods_num,$desc);
        $data = Common::arrayValueChange($data, 'goods_attr_value');
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => $this->getGoodsImg($data)
        ]);
    }
    private function addGoodsRating(&$data)
    {
        foreach ($data as $k =>$v){
            $goods_id=$v['goods_id'];
            $orderData=new OrderData();
            $data[$k]['order_rating']=$orderData->getGoodsRating($goods_id);
        }

    }

    public function searchGoodsTotal($name = '')
    {
        if (mb_strlen($name) > 1000) {
            return json(['code' => $this->f_code, 'msg' => '搜索名称太长']);
        }
        $shop = new ShopData();
        $data = $shop->searchGoodsTotal($name);
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => $data
        ]);
    }

    private function getGoodsImg($goods)
    {
        foreach ($goods as $k => $v) {
            $img = json_decode($v['goods_img']);
            $goods[$k]['goods_img'] = empty($img[0]) ?unserialize($v['goods_img']) : $img[0];
        }
        return $goods;
    }

    /**
     * 展示商品收藏状态
     * @param $user_id
     * @param $goods_id
     * @return \think\response\Json
     */
    public function showGoodsAttentionStatus($user_id, $goods_id)
    {
        $res = Common::getTableValue('drh_user_attention_goods', 'recover_status', ['user_id', 'goods_id'], [$user_id, $goods_id]);
        return json([
            'code' => $this->t_code,
            'msg' => 'OK',
            'data' => empty($res) ? 0 : $res
        ]);
    }


    public function showGoodsBargainInfo($shop_id, $goods_id, $user_id)
    {
        $res = Common::getTableValue(
            'drh_user_goods_bargain',
            ['bargain_total_money', 'bargain_once_money', 'bargain_total_num', 'bargain_num', 'id','title'],
            ['shop_id', 'goods_id', 'user_id'],
            [$shop_id, $goods_id, $user_id]
        );
        if (empty($res)) {
            return json([
                'code' => $this->f_code,
                'msg' => 'OK',
                'data' => []
            ]);
        } else {
            return json([
                'code' => $this->t_code,
                'msg' => 'OK',
                'data' => $res
            ]);
        }
    }

}